﻿using BackToCOS.IoC;
using log4net;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Myvas.AspNetCore.TencentCos;
using System;
using System.IO;
using Topshelf;

namespace BackToCOS
{
    class Program
    {
        static void Main(string[] args)
        {
            var configuration = new ConfigurationBuilder()
              .SetBasePath(Directory.GetCurrentDirectory())
              .AddJsonFile("appsettings.json", true, true)
              .AddJsonFile("appsettings.Development.json", true, true)
              .Build();
            log4net.Config.XmlConfigurator.ConfigureAndWatch(LogManager.CreateRepository("NETCoreRepository"), new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config"));

            IServiceCollection services = new ServiceCollection();

            services.AddLogging(builder => builder
             .AddConfiguration(configuration.GetSection("Logging")).AddLog4Net()
              );

            services.AddTencentCos(options =>
            {
                options.SecretId = configuration["TencentCos:SecretId"];
                options.SecretKey = configuration["TencentCos:SecretKey"];
            }).AddLogging();

            //注入配置
            services.AddSingleton<IConfiguration>(configuration);
            //注入服务
            services.AddSingleton<ITencentCosHandler, TencentCosHandler>();
            //用Autofac接管
            AutofacContainer.Build(services);
            HostFactory.Run(x =>
            {
                x.UseLog4Net();
                x.Service<BackupServiceRunner>();
                x.RunAsLocalSystem();
                x.SetDescription("备份到cos的服务");
                x.SetDisplayName("备份到cos的服务");
                x.SetServiceName("BackToCOS");
                x.EnablePauseAndContinue();
            });
        }
    }

}
